<script setup lang="ts">
import { requiredCheck } from "@/utils/verify";
import { ref, onMounted } from "vue";
import { addData, editData } from "@/api/modules/system/account";
import { sentry_online_port } from "@/api/modules/system-monitor/node-monitor";

interface Props {
  title?: string;
  sip?: string;
  maxCount?: number;
  rowData?: Object;
}

const props = withDefaults(defineProps<Props>(), {
  title: "新增账号",
  rowData: undefined,
  sip: "",
  maxCount:0
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ifaceAll, ifacemodeUpdate } from "@/api/modules/dynamic-camouflage/line-config";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {
    name: [...requiredCheck("请输入用户名")],
    password: [...requiredCheck("请输入密码")],
    passwordRe: [
      {
        validator: (rule, value, callback) => {
          if (value !== updateFormData.password) {
            callback(new Error("两次输入的密码不一致"));
          } else {
            callback();
          }
        },
        trigger: "blur"
      }
    ]
  },
  defaultData: {
    role: "1"
  }
});
const ifaces = ref([]);

async function getIface() {
  try {
    const res = await ifaceAll();
    console.log(res);
    if (res) {
      ifaces.value = Object.values(res);
      console.log(ifaces.value);
    }
    Object.values(res);
  } catch (error) {}
}

async function submitCb() {
  await submit({
    addApi: addData,
    updateApi: editData,
    sendData: {
      name: updateFormData.name,
      passwordRe: updateFormData.passwordRe || "",
      password: updateFormData.password || "",
      role: updateFormData.role,
      id: updateFormData?.id
    }
  });
  emits("success");
}

onMounted(async () => {
  let res = await sentry_online_port({
    sip: props.sip.replace("*", "")
  });
  console.log("res::", res);
});
const portList = ref([]);

async function openOnlinePort() {
  let res = await sentry_online_port({
    sip: props.sip.replace("*", "")
  });
  let arr = [];
  for (let i = 0; i < res.result?.length; i++) {
    if (res.result[i] != 0&&i!=10021) {
      arr.push(i);
    }
  }
  portList.value = arr;
  console.log("arr::", arr.length);
  console.log("arr::", props.maxCount);
}
</script>

<template>
  <div
    class="update-form mg-r-sm"
    style="display: inline-block"
    @click="
      openHandle({
        row: {
          ...rowData
        },
        id: props.rowData?.id
      });
      openOnlinePort();
    "
  >
    <slot>
      <el-button type="primary">新增</el-button>
    </slot>
  </div>
  <base-dialog :showConfirm="false" @confirm="submitCb()" width="600px" :title="title" v-model="showHandle">
    <div class="details">
      <div class="details-main">
        <el-form ref="updateFormRef" :model="updateFormData" :rules="updateFormRules" label-width="120px" label-suffix="：">
          <el-form-item label="" label-width="50px">
            <el-input :model-value="portList.join(',')" :rows="12" style="width: 90%" type="textarea"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <template #footer-more-button></template>
  </base-dialog>
</template>

<style lang="scss" scoped></style>
